<?php
function adminDisplay($template_name = null)
{
	global $smarty;
	if ( !$template_name  ) {
		$template_name = array_shift(explode('.',basename($_SERVER["SCRIPT_FILENAME"]))) . '.tpl';
	}
	if(isset($_SESSION['admin_login']) && $_SESSION['admin_login'] != ""){
		$smarty->assign("admin_login", $_SESSION['admin_login']);
	}
	$temp_arr = explode("/",$_SERVER['PHP_SELF']);
	$phpName = end($temp_arr);
	$smarty->assign("phpName", $phpName);
	$smarty->assign("adminIP", getIp());
	$smarty->assign("sysMsg", $_SESSION['sysMsg']);
	unset($_SESSION['sysMsg']);
	$smarty->assign("template_name", "management/".$template_name);
	$smarty->display("management/admin_layout.tpl");
}

function checkAdmin()
{
	$ip = getIP();
	global $admin_allow_ip, $permissionCfg;
	//if(!$admin_allow_ip[$ip]) die("You have no permission!");

	if(!isset($_SESSION['admin_login']) || $_SESSION['admin_login'] == ""){
		header("Location: /management/admin_login.php");
		die;
	}else{
		$grade = intval($_SESSION['admin_login']['grade']);
		if($grade > 1)
		{
			$temp_arr = explode("/",$_SERVER['PHP_SELF']);
			$phpName = end($temp_arr);
			if(array_key_exists($phpName, $permissionCfg[$grade])){
				return true;
			}else{
				$_SESSION['sysMsg'] = '无此权限';
				if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != ""){
					$forward = $_SERVER['HTTP_REFERER'];
					header("Location: $forward");
					die;
				}else{
					foreach($permissionCfg[$grade] as $key=>$value)
					{
						header("Location: /management/$key");
						die;
					}
				}
			}
		}else{
			return true;
		}
	}
}